#coding:utf-8

"""
    此代码作用: 利用scrapy框架, 通过已登录的Cookies值来爬取人人网信息
"""

import scrapy
import requests


class RenrenCookiesSpider(scrapy.Spider):
    name = "renren"
    allowed_domain = ["renren.com"]
    start_urls = [
        'http://www.renren.com/327550029/profile',
        'http://www.renren.com/410043129/profile'
    ]


    def start_requests(self):
        """该方法将读取start_urls内的地址,并为每个地址生成一个Request对象,交给scrapy下载并返回Response, 因此若需对请求头修改需重写此防范"""
        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Host": "www.renren.com",
            "Referer": "http://zhibo.renren.com/top",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"
        }
        cookies = {
            "anonymid": "johyh9c1g2zvhh",
            "_r01_": "1",
            "_de": "BF09EE3A28DED52E6B65F6A4705D973F1383380866D39FF5",
            "ln_uact": "mr_mao_hacker@163.com",
            "ln_hurl": "http://hdn.xnimg.cn/photos/hdn321/20180921/1130/main_Affp_c9750000af421986.jpg",
            "jebe_key": "2f4bdb41-e638-4ca9-a40e-8200758fa1ae%7Cc13c37f53bca9e1e7132d4b58ce00fa3%7C1542247744509%7C1%7C1542247746950",
            "depovince": "GW",
            "ick_login": "ecbd30e7-1116-4b33-891b-4dba64f8544f",
            "p": "9f4b06ab15206cbc9268f5a0384160a89",
            "first_login_flag": "1",
            "t": "e6c5c87232f0ec7ccfa077d3158172979",
            "societyguester": "e6c5c87232f0ec7ccfa077d3158172979",
            "id": "327550029",
            "loginfrom": "syshome",
            "wp_fold": "0",
            "JSESSIONID": "abcLlZdMGwumT8PiGoEDw",
            "ch_id": "10053",
            "xnsid": "a1c5c2d8",
            "jebecookies": "471f553c-b121-4c18-927f-86ca006ea04d|||||",
            "jebe_key": "2f4bdb41-e638-4ca9-a40e-8200758fa1ae%7Cc13c37f53bca9e1e7132d4b58ce00fa3%7C1542247744509%7C1%7C1543474160331"
        }

        for url in self.start_urls:
            # 源码代码
            # yield self.make_requests_from_url(url)

            yield scrapy.Request(url, headers=headers, cookies=cookies, callback=self.parse)


    def parse(self, response):
        title = response.xpath('//title/text()').extract_first()

        with open(title+".html","w") as f:
            f.write(response.body)

